import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import java.awt.Color;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

/**
 * A user interface to view the movies, add a new movie and to update an existing movie.
 * The list is a table with all the movie information in it. The TableModelListener listens to
 * any changes to the cells to modify the values for reach movie.
 * @author mmuppa, klw4z
 *
 */
public class MovieGUI extends JFrame implements ActionListener, TableModelListener
{

	private JButton btnList, btnSearch, btnAdd, reviewButton, addReview, submitReview, submitModifiedReview, editReview, deleteReview,
	addUser, btnAddUser, btnLogin, btnLoginSubmit, btnActor, btnAddActor;
	private JPanel pnlButtons, pnlContent, pnlReviews, pnlAddUser, imagePanel, postersPanel, pnlActor;
	private JTextArea userReview, modifyReview, reviewTextArea;
	private MovieDB db;
	private List<Movie> list;
	private List<Review> reviewList, currUserReviewList;
	private String[] columnNames = {"Title", "Year", "Director", "Synopsis", "Rating"};
	private String[] reviewColumns = {"Username", "Movie Title", "Movie Year", "Review", "Rating"};


	private Object[][] data;
	private Object[][] reviewData, currUserReviewData;
	private JTable table;
	private JScrollPane scrollPane, reviewScrollPane;
	private JPanel pnlSearch;
	private JLabel lblTitle, poster1;
	private JTextField txfTitle;
	private JButton btnTitleSearch;

	private JPanel pnlAdd, reviewButtonsPanel;
	private JLabel[] txfLabel = new JLabel[6];
	private JLabel[] addUserTxtLabel = new JLabel[3];
	private JLabel[] loginLabel = new JLabel[2];
	private JLabel[] actorLabel = new JLabel[3];
	private JTextField[] txfField = new JTextField[6];
	private JTextField[] addUserTxtField = new JTextField[3];
	private JTextField[] loginTxtField = new JTextField[2];
	private JTextField[] actorTxtField = new JTextField[3];
	private JButton btnAddMovie;
	private JPanel pnlUser;

	private JButton btnUserList;
	private List<User> userList;
	private String[] userColumn = {"Username", "Password", "Birthdate", "User Type"};
	private Object[][] userData;
	private JLabel lblNewLabel;
	private String myUser;
	//private User currUser;
	private JPanel pnlLogin;

	/**
	 * Creates the frame and components and launches the GUI.
	 */
	public MovieGUI() {
		super("Indie Films");
		myUser = "";
		db = new MovieDB();
		try {
			userList = db.getUser();
			currUserReviewList = db.getReviews();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try
		{
			list = db.getMovies();
			data = new Object[list.size()][columnNames.length];
			for (int i=0; i<list.size(); i++) {
				data[i][0] = list.get(i).getTitle();
				data[i][1] = list.get(i).getYear();
				data[i][2] = list.get(i).getDirector();
				data[i][3] = list.get(i).getSynopsis();
				data[i][4] = list.get(i).getRating();

			}

		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		createComponents();
		setVisible(true);
		setSize(1900,1000);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * Creates panels for Movie list, search, add and adds the corresponding 
	 * components to each panel.
	 */
	private void createComponents()
	{
		pnlButtons = new JPanel();
		pnlButtons.setBackground(Color.WHITE);
		btnList = new JButton("Movie List");
		btnList.addActionListener(this);

		btnSearch = new JButton("Movie Search");
		btnSearch.addActionListener(this);

		btnAdd = new JButton("Add Movie");
		btnAdd.addActionListener(this);

		reviewButton = new JButton("Read Reviews");
		reviewButton.addActionListener(this);

		btnUserList = new JButton("User List");
		btnUserList.addActionListener(this);

		addUser = new JButton("Create a new user");
		addUser.addActionListener(this);

		btnLogin = new JButton("Login");
		btnLogin.addActionListener(this);
		
		btnActor = new JButton("Add an actor");
		btnActor.addActionListener(this);

		pnlButtons.add(btnList);
		pnlButtons.add(btnSearch);
		pnlButtons.add(btnAdd);
		pnlButtons.add(reviewButton);
		//pnlButtons.add(btnUserList);
		pnlButtons.add(btnActor);
		pnlButtons.add(addUser);
		pnlButtons.add(btnLogin);
		getContentPane().add(pnlButtons, BorderLayout.NORTH);
		getContentPane().setBackground(Color.WHITE);

		//List Panel
		pnlContent = new JPanel();
		pnlContent.setLayout(new BorderLayout());
		pnlContent.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		pnlContent.setBackground(Color.WHITE);

		postersPanel = new JPanel(new FlowLayout());
		postersPanel.setBackground(Color.WHITE);
		poster1 = new JLabel();
		ImageIcon image1 = new ImageIcon("safety2.jpg", "IndieFilms");
		poster1.setIcon(image1);
		postersPanel.add(poster1);
		JLabel poster2 = new JLabel();
		ImageIcon image2 = new ImageIcon("southernwild.jpg", "IndieFilms");
		poster2.setIcon(image2);
		postersPanel.add(poster2);
		JLabel poster3 = new JLabel();
		ImageIcon image3 = new ImageIcon("everyday2.jpg", "IndieFilms");
		poster3.setIcon(image3);
		postersPanel.add(poster3);
		JLabel poster4 = new JLabel();
		ImageIcon image4 = new ImageIcon("arcadia.jpg", "IndieFilms");
		poster4.setIcon(image4);
		postersPanel.add(poster4);
		JLabel poster5 = new JLabel();
		ImageIcon image5 = new ImageIcon("martha2.jpg", "IndieFilms");
		poster5.setIcon(image5);
		postersPanel.add(poster5);
		JLabel poster6 = new JLabel();
		ImageIcon image6 = new ImageIcon("anotherearth2.jpg", "IndieFilms");
		poster6.setIcon(image6);
		postersPanel.add(poster6);
		JLabel poster7 = new JLabel();
		ImageIcon image7 = new ImageIcon("oliversherman2.jpg", "IndieFilms");
		poster7.setIcon(image7);
		postersPanel.add(poster7);
		pnlContent.add(postersPanel,BorderLayout.SOUTH);

		imagePanel = new JPanel();
		lblNewLabel = new JLabel();
		ImageIcon image = new ImageIcon("indiefilmspic.png", "IndieFilms");
		lblNewLabel.setIcon(image);
		imagePanel.add(lblNewLabel);
		imagePanel.setSize(new Dimension(100,100));
		imagePanel.validate();
		pnlContent.add(imagePanel, BorderLayout.WEST);

		table = new JTable(data, columnNames);
		table.setRowHeight(30);
		//table.setFont(new Font("Sans Serif", Font.BOLD, 20));
		TableColumnModel columnModel = table.getColumnModel();
		columnModel.getColumn(0).setPreferredWidth(20);
		columnModel.getColumn(1).setPreferredWidth(20);
		columnModel.getColumn(2).setPreferredWidth(40);
		columnModel.getColumn(3).setPreferredWidth(180);
		columnModel.getColumn(4).setPreferredWidth(10);
		//table.setPreferredSize(new Dimension(500,500));
		scrollPane = new JScrollPane(table);
		scrollPane.getViewport().setBackground(Color.WHITE);
		pnlContent.add(scrollPane,BorderLayout.CENTER);
		pnlContent.validate();
		table.getModel().addTableModelListener(this);

		//Search Panel
		pnlSearch = new JPanel();
		lblTitle = new JLabel("Enter Title: ");
		txfTitle = new JTextField(25);
		btnTitleSearch = new JButton("Search");
		btnTitleSearch.addActionListener(this);
		pnlSearch.add(lblTitle);
		pnlSearch.add(txfTitle);
		pnlSearch.add(btnTitleSearch);

		//Add Panel
		pnlAdd = new JPanel();
		pnlAdd.setLayout(new GridLayout(7, 0));
		String labelNames[] = {"Enter Title: ", "Enter Year: ", "Enter Director: ", "Enter Synopsis: ", "Enter Rating (Optional): "};
		for (int i=0; i<labelNames.length; i++) {
			JPanel panel = new JPanel();
			txfLabel[i] = new JLabel(labelNames[i]);
			txfField[i] = new JTextField(25);
			panel.add(txfLabel[i]);
			panel.add(txfField[i]);
			pnlAdd.add(panel);
		}
		JPanel panel = new JPanel();
		btnAddMovie = new JButton("Add");
		btnAddMovie.addActionListener(this);
		panel.add(btnAddMovie);
		pnlAdd.add(panel);

		//Review Panel
		pnlReviews = new JPanel();
		pnlReviews.setLayout(new BorderLayout());
		reviewButtonsPanel = new JPanel();
		reviewButtonsPanel.setLayout(new FlowLayout());
		addReview = new JButton("Add a review");
		addReview.addActionListener(this);
		reviewButtonsPanel.add(addReview);
		editReview = new JButton("Edit my review");
		editReview.addActionListener(this);
		reviewButtonsPanel.add(editReview);
		deleteReview = new JButton("Delete my review");
		deleteReview.addActionListener(this);
		reviewButtonsPanel.add(deleteReview);
		reviewScrollPane = new JScrollPane();
		pnlReviews.add(reviewScrollPane, BorderLayout.CENTER);

		// UserPanel Testing
		pnlUser = new JPanel();
		//userList = db.getUser();
		userData = new Object[userList.size()][userColumn.length];
		for(int i = 0; i < userList.size(); i++) {
			userData[i][0] = userList.get(i).getUsername();
			userData[i][1] = userList.get(i).getPassword();
			userData[i][2] = userList.get(i).getBirthdate();
			userData[i][3] = userList.get(i).getUserType();
		}
		JPanel pnlUserList = new JPanel();
		JTable userTable = new JTable(userData, userColumn);
		JScrollPane userSP = new JScrollPane(userTable);
		pnlUserList.add(userSP);

		//add user
		pnlAddUser = new JPanel();
		pnlAddUser.setLayout(new GridLayout(4,1));
		String addUserLabel[] = {"Enter username: ", "Enter password: ", "Enter birthdate: "};
		for(int i = 0; i < addUserLabel.length; i++) {
			JPanel p = new JPanel();
			addUserTxtLabel[i] = new JLabel(addUserLabel[i]);
			addUserTxtField[i] = new JTextField(25);
			p.add(addUserTxtLabel[i]);
			p.add(addUserTxtField[i]);
			pnlAddUser.add(p);
		}
		JPanel p = new JPanel();
		btnAddUser = new JButton("Add User");
		btnAddUser.addActionListener(this);
		p.add(btnAddUser);
		pnlAddUser.add(p);

		//Login
		pnlLogin = new JPanel(new GridLayout(3,1));
		String strLoginLabel[] = {"Username", "Password"};
		for(int i = 0; i < loginLabel.length; i++) {
			JPanel temp = new JPanel();
			loginLabel[i] = new JLabel(strLoginLabel[i]);
			loginTxtField[i] = new JTextField(25);
			temp.add(loginLabel[i]);
			temp.add(loginTxtField[i]);
			pnlLogin.add(temp);
		}
		btnLoginSubmit = new JButton("Login");
		btnLoginSubmit.addActionListener(this);
		JPanel temp = new JPanel();
		temp.add(btnLoginSubmit);
		pnlLogin.add(temp);
		
		//Add Actor Panel
		pnlActor = new JPanel(new GridLayout(4,1));
		String strActorLabel[] = {"Movie Title", "Movie Year", "Actor"};
		for(int i = 0; i< strActorLabel.length; i++) {
			JPanel temp1 = new JPanel();
			actorLabel[i] = new JLabel(strActorLabel[i]);
			actorTxtField[i] = new JTextField(25);
			temp1.add(actorLabel[i]);
			temp1.add(actorTxtField[i]);
			pnlActor.add(temp1);
		}
		btnAddActor = new JButton("Add Actor");
		btnAddActor.addActionListener(this);
		JPanel temp1 = new JPanel();
		temp1.add(btnAddActor);
		pnlActor.add(temp1);
		
		getContentPane().add(pnlContent, BorderLayout.CENTER);
	}

	private void addImages()
	{
		imagePanel.add(lblNewLabel);
		imagePanel.validate();
		pnlContent.add(imagePanel, BorderLayout.WEST);
		pnlContent.add(postersPanel,BorderLayout.SOUTH);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		new MovieGUI();

	}

	/**
	 * Event handling to change the panels when different tabs are clicked,
	 * add and search buttons are clicked on the corresponding add and search panels.
	 */
	//@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnList) {
			try {
				list = db.getMovies();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			data = new Object[list.size()][columnNames.length];
			for (int i=0; i<list.size(); i++) {
				data[i][0] = list.get(i).getTitle();
				data[i][1] = list.get(i).getYear();
				data[i][2] = list.get(i).getDirector();
				data[i][3] = list.get(i).getSynopsis();
				data[i][4] = list.get(i).getRating();
			}
			pnlContent.removeAll();
			table = new JTable(data, columnNames);
			table.setRowHeight(50);
			table.getModel().addTableModelListener(this);
			//table.setRowHeight(0,50);
			scrollPane = new JScrollPane(table);
			pnlContent.add(scrollPane);
			addImages();
			pnlContent.revalidate();
			this.repaint();

		} else if (e.getSource() == btnSearch) {
			pnlContent.removeAll();
			pnlContent.add(pnlSearch);
			pnlContent.revalidate();
			this.repaint();
		} else if (e.getSource() == btnAdd) {
			//			System.out.println(db.UserExists("tinj13"));
			//			System.out.println(db.UserExists("123"));

			pnlContent.removeAll();
			pnlContent.add(pnlAdd);
			pnlContent.revalidate();
			this.repaint();

		} else if (e.getSource() == btnTitleSearch) {
			String title = txfTitle.getText();
			if (title.length() > 0) {
				list = db.getMovies(title);
				data = new Object[list.size()][columnNames.length];
				for (int i=0; i<list.size(); i++) {
					data[i][0] = list.get(i).getTitle();
					data[i][1] = list.get(i).getYear();
					data[i][2] = list.get(i).getDirector();
					data[i][3] = list.get(i).getSynopsis();
					data[i][4] = list.get(i).getRating();
				}
				pnlContent.removeAll();
				table = new JTable(data, columnNames);
				table.getModel().addTableModelListener(this);
				scrollPane = new JScrollPane(table);
				pnlContent.add(scrollPane);
				pnlContent.revalidate();
				this.repaint();
			}
		} 
		else if (e.getSource() == addReview) {
			pnlContent.removeAll();
			userReview = new JTextArea(15,pnlContent.getY());
			JLabel l = new JLabel("Write your review here: \n\n");
			JPanel userReviewPanel = new JPanel();
			userReviewPanel.setLayout(new BorderLayout());
			userReviewPanel.add(userReview, BorderLayout.CENTER);
			userReviewPanel.add(l,BorderLayout.NORTH);
			submitReview = new JButton("Submit Review");
			submitReview.addActionListener(this);
			userReviewPanel.add(submitReview, BorderLayout.SOUTH);
			pnlContent.add(userReviewPanel);
			addImages();
			pnlContent.revalidate();
			this.repaint();
		}
		else if (e.getSource()==submitReview)
		{
			int selectedRow = table.getSelectedRow();
			Review r = new Review(myUser, (String)data[selectedRow][0], (Integer)data[selectedRow][1],userReview.getText(), 1);
			for (User u : userList)
			{
				if(u.getUsername().equals(myUser))
				{
					u.addReview(r);
				}
			}
			//System.out.println("Review id: " + r.getReviewID());
			db.addReview(r);
			JOptionPane.showMessageDialog(null, "Added Successfully!");
			for (int i=0; i<txfField.length; i++) {
				txfField[i].setText("");
			}
		}
		else if (e.getSource() == btnAddMovie) {
			if(!myUser.equals("")) {
				if(!db.UserExists(txfField[2].getText())) {
					JOptionPane.showMessageDialog(null, "Please have the director create an account first");
				} else {

					Movie movie = new Movie(txfField[0].getText(), Integer.parseInt(txfField[1].getText())
							,txfField[2].getText(), txfField[3].getText(), Integer.parseInt(txfField[4].getText()));
					Director d = new Director(txfField[2].getText(), "");
					db.addDirector(d, txfField[2].getText());
					db.addMovie(movie);
					JOptionPane.showMessageDialog(null, "Added Successfully!");
					for (int i=0; i<txfField.length-1; i++) {
						txfField[i].setText("");
					}
				}
			} else if(myUser.equals("")){
				JOptionPane.showMessageDialog(null, "Please login in first before adding a movie");
			}
			//UserList
		} else if(e.getSource() == btnUserList) {
			pnlUser = new JPanel();
			//userList = db.getUser();
			userData = new Object[userList.size()][userColumn.length];
			for(int i = 0; i < userList.size(); i++) {
				userData[i][0] = userList.get(i).getUsername();
				userData[i][1] = userList.get(i).getPassword();
				userData[i][2] = userList.get(i).getBirthdate();
				userData[i][3] = userList.get(i).getUserType();
			}
			JPanel pnlUserList = new JPanel();
			JTable userTable = new JTable(userData, userColumn);
			JScrollPane userSP = new JScrollPane(userTable);
			pnlUserList.add(userSP);
			pnlContent.removeAll();
			pnlContent.add(pnlUserList);
			pnlContent.revalidate();
			this.repaint();
		} else if (e.getSource() == reviewButton) { //see reviews
			if(displayReviews())
			{
				pnlContent.removeAll();
				pnlReviews.removeAll();
				reviewScrollPane = new JScrollPane(reviewTextArea);
				pnlReviews.add(reviewButtonsPanel, BorderLayout.SOUTH);
				pnlReviews.add(reviewScrollPane);
				pnlReviews.revalidate();
				pnlContent.add(pnlReviews);
				addImages();
				pnlContent.revalidate();
				this.repaint();
			}
		} else if (e.getSource() == addUser) {	
			//add user
			pnlContent.removeAll();
			pnlContent.add(pnlAddUser);
			pnlContent.revalidate();
			this.repaint();
		} else if(e.getSource() == btnAddUser) {
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
			try {
				d = sdf.parse(addUserTxtField[2].getText());
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			User u = new User(addUserTxtField[0].getText(), addUserTxtField[1].getText(), d);
			db.addUser(u);
			JOptionPane.showMessageDialog(null, "Added Successfully!");
		}
		else if(e.getSource() == editReview) 
		{
			if (myUser == null) JOptionPane.showMessageDialog(null, "Please log in.");
			else{
				pnlContent.removeAll();
				pnlReviews.removeAll();
				modifyReview = new JTextArea(15,pnlContent.getY());
				JPanel modifyReviewPanel = new JPanel();
				modifyReviewPanel.setLayout(new BorderLayout());
				submitModifiedReview = new JButton("Submit Modified Review");
				submitModifiedReview.addActionListener(this);
				currUserReviewData = new Object[currUserReviewList.size()][reviewColumns.length];
				int counter = 0;
				for (int i=0; i<currUserReviewList.size(); i++) {
					if (currUserReviewList.get(i).getReviewUsername().equals(myUser))
					{
						//System.out.println(currUserReviewList.get(i).getReviewUsername());
						currUserReviewData[counter][0] = currUserReviewList.get(i).getReviewUsername();
						currUserReviewData[counter][1] = currUserReviewList.get(i).getReviewMovieTitle();
						currUserReviewData[counter][2] = currUserReviewList.get(i).getReviewMovieYear();
						currUserReviewData[counter][3] = currUserReviewList.get(i).getComment();
						currUserReviewData[counter][4] = currUserReviewList.get(i).getReviewRating();
						counter++;
					}
				}
	
				if (currUserReviewData[0][0] != null)
				{
					modifyReview.append(""+currUserReviewData[0][3]);
				}
				JLabel l = new JLabel("Edit your review for movie " + currUserReviewData[0][1] + " here: \n\n");
				modifyReviewPanel.add(l,BorderLayout.NORTH);
				modifyReviewPanel.add(modifyReview, BorderLayout.CENTER);
				modifyReviewPanel.add(submitModifiedReview, BorderLayout.SOUTH);
				pnlContent.add(modifyReviewPanel);
				addImages();
				pnlContent.revalidate();
				this.repaint();
			}
		}
		else if(e.getSource()==submitModifiedReview)
		{
			int selectedRow = table.getSelectedRow();
			for (User u : userList)
			{
				if(u.getUsername().equals(myUser))
				{
					//System.out.println("User: " + u.getUsername());
					for (Review r : currUserReviewList)
					{
						if (r.getReviewUsername().equals(myUser))
						{
							//System.out.println("Reviewed movies: " + r.getReviewMovieTitle());
							if (r.getReviewMovieTitle().equals((String)data[selectedRow][0]))
							{
								//System.out.println("Review id: " + r.getReviewID());
								db.updateReview(r.getReviewID(), modifyReview.getText());
							}
						}
					}
					break;
				}
			}
			JOptionPane.showMessageDialog(null, "Review modified!");
		}
		else if(e.getSource()==deleteReview)
		{
			//System.out.println("In deleteReview");
			if (myUser == null) JOptionPane.showMessageDialog(null, "Please log in.");
			else{
				int selectedRow = table.getSelectedRow();
				for (User u : userList)
				{
					//System.out.println("User: " + u.getUsername());
					if(u.getUsername().equals(myUser))
					{
						//System.out.println("Current User: " + u.getUsername());
						Review theReview = null;
						for (Review r : currUserReviewList)
						{
							//System.out.println("Reviewed movies: " + r.getReviewMovieTitle());
							if (r.getReviewMovieTitle().equals((String)data[selectedRow][0]) && r.getReviewUsername().equals(myUser))
							{
								//System.out.println("Review id: " + r.getReviewID());
								theReview = r;
								db.deleteReview(r.getReviewID());
							}
						}
						if(theReview != null) u.getUserReviews().remove(theReview);
						break;
					}
				}
				JOptionPane.showMessageDialog(null, "Review deleted.");
				pnlContent.removeAll();
				pnlReviews.removeAll();
				displayReviews();
				addImages();
				reviewScrollPane = new JScrollPane(reviewTextArea);
				pnlReviews.add(reviewButtonsPanel, BorderLayout.SOUTH);
				pnlReviews.add(reviewScrollPane);
				pnlReviews.revalidate();
				pnlContent.add(pnlReviews);
				pnlContent.revalidate();
				this.repaint();
			}
		} else if (e.getSource() == btnLogin) {
			pnlContent.removeAll();
			pnlContent.add(pnlLogin);
			pnlContent.revalidate();
			this.repaint();
		} else if (e.getSource() == btnLoginSubmit) {
			if(!db.UserExists(loginTxtField[0].getText())) {
				JOptionPane.showMessageDialog(null, "User does not exist");
				loginTxtField[0].setText("");
				loginTxtField[1].setText("");

			} else {
				if(loginTxtField[1].getText().equals(db.getPassword(loginTxtField[0].getText()))) {
					myUser = loginTxtField[0].getText();
					JOptionPane.showMessageDialog(null, "Login Successful");
					pnlButtons.remove(btnLogin);
					pnlContent.revalidate();
					this.repaint();
					System.out.println("Currently Logged in as " + myUser);
				} else {
					System.out.println();
					JOptionPane.showMessageDialog(null, "Incorrect password");
					loginTxtField[1].setText("");
				}
			}
		} else if(e.getSource() == btnActor) {
			pnlContent.removeAll();
			pnlContent.add(pnlActor);
			pnlContent.revalidate();
			this.repaint();
		} else if(e.getSource() == btnAddActor) {
			Movie m = new Movie(actorTxtField[0].getText(), Integer.parseInt(actorTxtField[1].getText()));
			if(!db.UserExists(actorTxtField[2].getText())) {
				JOptionPane.showMessageDialog(null, "Please the actor create an account first");
			} else if(!myUser.equals("") && myUser.equals(db.getMovieDirector(m))) {
			db.addActor(actorTxtField[2].getText(), m);
			JOptionPane.showMessageDialog(null, "Actor added");
			} else if(myUser.equals("")){
				JOptionPane.showMessageDialog(null, "Please Login In");
			} else if(!myUser.equals(db.getMovieDirector(m))) {
				JOptionPane.showMessageDialog(null, "You can only add an actor if you are the director of that movie");
			}
		}
	}

	/**
	 * Event handling for any cell being changed in the table.
	 */
	//@Override
	public void tableChanged(TableModelEvent e) {
		int row = e.getFirstRow();
		int column = e.getColumn();
		TableModel model = (TableModel)e.getSource();
		String columnName = model.getColumnName(column);
		Object data = model.getValueAt(row, column);

		db.updateMovie(row, columnName, data);	
	}

	private boolean displayReviews()
	{
		boolean result = false;
		try
		{
			int selectedRow = table.getSelectedRow();
			currUserReviewList = db.getReviews(data[selectedRow][0]);
			reviewData = new Object[currUserReviewList.size()][reviewColumns.length];
			for (int i=0; i<currUserReviewList.size(); i++) {
				reviewData[i][0] = currUserReviewList.get(i).getReviewUsername();
				reviewData[i][1] = currUserReviewList.get(i).getReviewMovieTitle();
				reviewData[i][2] = currUserReviewList.get(i).getReviewMovieYear();
				reviewData[i][3] = currUserReviewList.get(i).getComment();
				reviewData[i][4] = currUserReviewList.get(i).getReviewRating();
			}
			pnlContent.removeAll();
			reviewTextArea = new JTextArea();
			if (currUserReviewList.size() == 0) reviewTextArea.append("No reviews for this movie.\n\n");
			else {
				for(int i = 0; i<currUserReviewList.size();i++)
				{
					reviewTextArea.append("Username: " + reviewData[i][0]);
					reviewTextArea.append("\nMovie Title: " + reviewData[i][1]);
					reviewTextArea.append("\nMovie Year: " + reviewData[i][2]);
					reviewTextArea.append("\nReview: " + reviewData[i][3]);
					reviewTextArea.append("\nRating: " + reviewData[i][4]);
					reviewTextArea.append("\n\n");
				}
			}
			result = true;
		}
		catch(Exception exc)
		{
			JOptionPane.showMessageDialog(null, "No movie selected.");
		}
		return result;
	}
}
